<template>
  <el-form
    ref="customForm"
    :model="formData"
    :rules="formRules"
    label-width="110px"
  >
    <el-form-item v-if="action === 'edit'" prop="id">
      <input v-model="formData.id" type="hidden">
      <input v-model="formData.configId" type="hidden">
      <input v-model="formData.pid" type="hidden">
    </el-form-item>
    <el-form-item label="菜单类型" prop="type">
      <MpMenuType v-model="formData.type" />
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="formData.name" type="text" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="Key" prop="key">
      <el-input v-model="formData.key" type="text" placeholder="click等点击类型必须" />
    </el-form-item>
    <el-form-item label="Url" prop="url">
      <el-input v-model="formData.url" type="text" placeholder="view、miniprogram类型必须" />
    </el-form-item>
    <el-form-item label="MideaId" prop="mediaId">
      <el-input v-model="formData.mediaId" type="text" placeholder="media_id类型和view_limited类型必须" />
    </el-form-item>
    <el-form-item label="AppId" prop="appId">
      <el-input v-model="formData.appId" type="text" placeholder="miniprogram类型必须" />
    </el-form-item>
    <el-form-item label="PagePath" prop="pagePath">
      <el-input v-model="formData.pagePath" type="text" placeholder="miniprogram类型必须" />
    </el-form-item>
  </el-form>
</template>
<script>
import { get, save, update } from '@/api/wx/mp/mp-menu';
import MpMenuType from '@/components/wechat/mp-menu-type';

export default {
  name: 'WxMpConfigForm',
  components: {
    MpMenuType
  },
  props: {
    action: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      formRules: {
        type: [
          { required: true, message: '菜单类型必选', trigger: 'change' }
        ],
        name: [
          { required: true, message: '名称不能为空！', trigger: 'blur' }
        ]
      },
      formData: {
        id: null,
        configId: null,
        pid: null,
        type: null,
        name: null,
        key: null,
        url: null,
        mediaId: null,
        appId: null,
        pagePath: null
      }
    };
  },
  methods: {
    getInfo(id) {
      get(id).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.formData = result.data;
        } else {
          this.$notify.error({
            title: result.code,
            message: result.msg
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.customForm.validate((valid) => {
        if (valid) {
          if (this.action === 'add') {
            save(this.formData).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.action === 'edit') {
            update(this.formData).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult(obj) {
      if (obj.code === 0) {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'success'
        });
        this.$parent.$parent.getMenuData();
        this.$parent.$parent.formInitOption.showModal = false;
      } else {
        this.$notify.error({
          title: obj.code,
          desc: obj.msg
        });
      }
    },
    reset() {
      this.$refs.customForm.resetFields();
    },
    init(configId, pid) {
      this.formData.configId = configId;
      this.formData.pid = pid;
    }
  }
};
</script>
